<?php

namespace controller;

/**
 * Description of ShopAdmin_Configuration
 *
 * @author Damian
 */
class ShopAdmin_Configuration extends Seller {

    public function index() {
        $layoutView = $this->getLayoutView();
        $layoutView->set("paneltitle", "Skonfiguruj swój sklep");

        $view = $this->getActionView();
        $view->set("shopname", $this->shop->Name);
        $this->render();
    }

    public function payments() {
        if (sizeof($_POST) > 0) {
            $Name = \library\RequestMethods::post("name");
            $active = \library\RequestMethods::post("active");
            $Description = \library\RequestMethods::post("shortDesription");
            $emailDescription = \library\RequestMethods::post("fullDescritpion");
            $payment = "";
            if (\library\RequestMethods::post("id")) {
                $paymentCriteria = new \model\Criteria();
                $paymentCriteria->from(\model\TblPayment::getModel()->getTable())
                        ->where(\model\TblPayment::ID, "=", \library\RequestMethods::post("id"))
                        ->where(\model\TblPayment::SHOP_ID, "=", $this->shop->ShopID, false, " and ");
                $payment = \model\TblPayment::getModel()->first($paymentCriteria);
            } else {
                $payment = new \model\TblPayment();
            }
            $payment->Name = $Name;
            $payment->ShopID = $this->shop->ShopID;
            $payment->Active = intval($active);
            $payment->Description = trim($Description);
            $payment->MailContent = trim($emailDescription);
            if ($payment->validate()) {
                $payment->save();
            }
            else {
                var_dump($payment->getErrors());
                var_dump($_POST);
            }
            return;
        }
        $layoutView = $this->getLayoutView();
        $layoutView->set("paneltitle", "Metody płatności");
        $paymentsCriteria = new \model\Criteria();
        $paymentsCriteria->from(\model\TblPayment::getModel()->getTable())
                ->where(\model\TblPayment::SHOP_ID, "=", $this->shop->ShopID);
        $payments = \model\TblPayment::getModel()->all($paymentsCriteria);
        $view = $this->getActionView();
        if ($payments) {
            $view->set("payments", $payments);
        }
        $this->render();
    }

    public function delivery() {
        if (sizeof($_POST) > 0) {            
            $payment = "";
            if (\library\RequestMethods::post("paymentID")) {
                $Name = \library\RequestMethods::post("methodName");
                $active = \library\RequestMethods::post("methodActive");
                $Description = \library\RequestMethods::post("methodDescription");
                $Price = \library\RequestMethods::post("methodPrice");
                $paymentCriteria = new \model\Criteria();
                $paymentCriteria->from(\model\TblDeliveryMethod::getModel()->getTable())
                        ->where(\model\TblDeliveryMethod::ID, "=", \library\RequestMethods::post("paymentID"))
                        ->where(\model\TblDeliveryMethod::SHOP_ID, "=", $this->shop->ShopID, false, " and ");
                $Delivery = \model\TblDeliveryMethod::getModel()->first($paymentCriteria);
                $Delivery->Name = $Name;
                $Delivery->ShopID = $this->shop->ShopID;
                $Delivery->Active = intval($active);
                $Delivery->Description = trim($Description);
                $Delivery->Price = $Price;
                if ($Delivery->validate()) {
                    $Delivery->save();
                }
            } else {
                $Name = \library\RequestMethods::post("name");
                $active = \library\RequestMethods::post("active");
                $Description = \library\RequestMethods::post("fullDescritpion");
                var_dump($Description);
                $Price = \library\RequestMethods::post("price",0);
                $Delivery = new \model\TblDeliveryMethod();
                $Delivery->Name = $Name;
                $Delivery->ShopID = $this->shop->ShopID;
                $Delivery->Active = intval($active);
                $Delivery->Description = trim($Description);
                $Delivery->Price = $Price;
                // var_dump($_POST);
                if ($Delivery->validate()) {
                    $Delivery->save();
                    return;
                }               
            }
           // 
        }
        $layoutView = $this->getLayoutView();
        $layoutView->set("paneltitle", "Metody dostawy");
        $DeliveryMethodCriteria = new \model\Criteria();
        $DeliveryMethodCriteria->from(\model\TblDeliveryMethod::getModel()->getTable())
                ->where(\model\TblDeliveryMethod::SHOP_ID, "=", $this->shop->ShopID);
        $deliveryMethods = \model\TblPayment::getModel()->all($DeliveryMethodCriteria);
        $view = $this->getActionView();
        if ($deliveryMethods) {
            $view->set("deliveryMethods", $deliveryMethods);
        }
        $this->render();
    }

}
